package com.example.myapplication.music;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.R;

public class frag1 extends Fragment {
    private View view;
    //创建歌曲的String数组和歌手图片的int数组
    public String[] name={"邓紫棋——光年之外","蔡健雅——红色高跟鞋","Taylor Swift——Love Story"};
    public static int[] icons={R.drawable.pic_default_music,R.drawable.pic_default_music,R.drawable.pic_default_music};
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view=inflater.inflate(R.layout.music_list,null);
       ListView  listView=view.findViewById(R.id.list_music);

        MyBaseAdapter adapter=new MyBaseAdapter();

        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(frag1.this.getContext(), MusicActivity.class);

                intent.putExtra("name",name[position]);
                intent.putExtra("position",String.valueOf(position));

                startActivity(intent);
            }
        });

        return view;
    }

    class MyBaseAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View view1=View.inflate(frag1.this.getContext(),R.layout.music_item,null);
            TextView textView=view1.findViewById(R.id.item_name);
            ImageView imageView=view1.findViewById(R.id.iv);

            textView.setText(name[position]);
            imageView.setImageResource(icons[position]);

            return view1;
        }
    }



}
